package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    //分页查询
    @Override
    public PageBean queryString(Integer page, Integer pageSize, String name, String no, Integer degree, Integer clazzId) {
        PageHelper.startPage(page, pageSize);
        Page<Student> p = (Page<Student>) studentMapper.list(name, no, degree, clazzId);
        return new PageBean(p.getResult(), p.getTotal());
    }

    //删除学员
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    //添加学员
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    //根据id查询
    @Override
    public Student geById(Integer id) {
        return studentMapper.findById(id);
    }

    //修改学员
    @Override
    public void updateById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    //违纪处理
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.updateViolation(id, score);
    }

    //学员人数统计
    @Override
    public Map<String, List<Object>> getStudentData() {
        List<Map<String, Object>> list = studentMapper.getStudentData();

        //map():对元素进行加工，返回对应的新流

        List<Object> names = list.stream().map(map -> map.get("name"))
                .collect(Collectors.toList());
        List<Object> counts = list.stream().map(map -> map.get("count"))
                .collect(Collectors.toList());

        Map<String, List<Object>> map = new HashMap<>();
        map.put("categoryList",names);
        map.put("dataList",counts);
        return map;
    }
}
